﻿package com.saint.elasticity 
{
	import com.saint.utils.Logger;
	import flash.display.Sprite;
	import flash.geom.Point;
	/**
	 * creates and manages the points in and elastic grid
	 * @author kembera
	 */
	public class ElasticGridManager
	{
		private var _buttons:Array = new Array();
		private var _cols:uint;
		private var _rows:uint;
		private var _gridHolder:Sprite;
		private var _tileW:Number = 200;
		private var _tileH:Number = 200;
		private var _buttonAlpha = 0.5;
		private var resetPositions:Array = new Array();
		
		public function ElasticGridManager(gridHolder:Sprite) 
		{
			Logger.info("ElasticGridManager.ElasticGridManager " );
			_gridHolder = gridHolder;
		}
		
		public function doElasticity():void
		{
			for (var i:Number = 0 ; i < _buttons.length; i++)
			{
				_buttons[i].enterFrameEvents(null);
			}
		}
		
		public function createElasticGrid(cols:uint, rows:uint):Array
		{
			Logger.info("ElasticGridManager.createElasticGrid " + cols + ", " + rows);		
					
			_cols = cols;
			_rows = rows;
			var dragBtn:IAnchorPoint;
			
			for (var i:Number = 0; i < (_cols + 1) * (_rows + 1); i++)
			{			
				/* get the left side outer buttons == 0*/
				if (i % (_cols + 1) == 0)
				{
					if (i == 0 )
					{
						/* top left corner */
						dragBtn = new FixedAnchorPoint();
					}else if ( i == (_cols + 1) * _rows) {
						/* bottom left corner */
						dragBtn = new FixedAnchorPoint();						
					}else {
						/* left side */
						dragBtn = new ElasticObject();
					}
				}else 			
				/* get the right side buttons  == _cols*/
				if ((i % (_cols + 1) == _cols))
				{					
					if (i == _cols  )
					{					
						/* top right corner */
						dragBtn = new FixedAnchorPoint();
					}else if (i == _cols + (_cols + 1) * _rows) {		
						/* bottom left corner */
						dragBtn = new FixedAnchorPoint();
					}else{
						/* right side */	
						dragBtn = new ElasticObject();
					}
				}
				else {
					/* middle bits */
					dragBtn = new ElasticObject();
				}
							
			
				// make new IAnchorpiont				
				_buttons.push(dragBtn);
			}
			
			layoutGrid(_cols, _rows);
			return _buttons;
			
		}
		
		
		
					
		/**
		 * lays out the buttons on the scope
		 * 
		 * 	0	-	1	-	2	-	3	-	4
		 * 
		 * 	5	-	6	-	7	-	9	-	10
		 * 
		 * 	1	-	12	-	13	-	14	-	15
		 * 
		 * 
		 */
		public function layoutGrid(_cols:uint, _rows:uint):void
		{
			Logger.info("StretchyImage.layoutButtons");
			
			
			var leftBut:IAnchorPoint;
			var rightBut:IAnchorPoint;
			var topBut:IAnchorPoint;
			var bottomBut:IAnchorPoint;
			var currentElasticObj:ElasticObject;
			var index:Number;
			var pnt:Point;
			
			for (var i:Number = 0; i <= _rows; i++)
			{
				for (var n:Number = 0; n <= _cols; n++)
				{
					index = n + (i * (_cols + 1));
					pnt = new Point(n * _tileW,i * _tileH)
					_buttons[index].x = pnt.x;  
					_buttons[index].y = pnt.y;  
					_buttons[index].alpha = buttonAlpha; 
					resetPositions.push(pnt);
					_gridHolder.addChild(_buttons[index]);	
					
					/* set the top row */
					if (i == 0)
					{
						/* topLeftCorner */
						if (n == 0) {  }
						else
						
						/* topRight */
						if (n == _cols) {  }
						
						else						
						{
							/* top row */			
							currentElasticObj = _buttons[index];
							leftBut = _buttons[index - 1];
							rightBut = _buttons[index + 1];							
							currentElasticObj.attachSpringFrom(leftBut);
							currentElasticObj.attachSpringFrom(rightBut);
							currentElasticObj.init();						
						};
					}
					else
					/* set the bottom row */
					if (i == _rows )
					{
						/* bottomLeftCorner */
						if (n == 0) {  }
						
						else
						
						/* bottmoRight */
						if (n == _cols) { }
						
						else						
						{										
							currentElasticObj = _buttons[index];
							leftBut = _buttons[index - 1];
							rightBut = _buttons[index + 1];							
							currentElasticObj.attachSpringFrom(leftBut);
							currentElasticObj.attachSpringFrom(rightBut);
							currentElasticObj.init();	
						};
					}
					else
					/* set the left sides */
					if (n == 0)
					{
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();	
						
					}
					else
					/* set the right sides */
					if (n == _cols )
					{
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();	
					}
					/* set the interior ponts */
					else
					{		
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];
						leftBut = _buttons[index - 1];
						rightBut = _buttons[index + 1];	
						
						currentElasticObj.attachSpringFrom(leftBut);
						currentElasticObj.attachSpringFrom(rightBut);							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();				
					}
				}
			}
			
		}
		
		public function get tileW():Number { return _tileW; }		
		public function set tileW(value:Number):void 
		{
			_tileW = value;
		}
		
		public function get tileH():Number { return _tileH; }		
		public function set tileH(value:Number):void 
		{
			_tileH = value;
		}
		
		public function get buttonAlpha():Number { return _buttonAlpha; }		
		public function set buttonAlpha(value:Number):void 
		{
			_buttonAlpha = value;
		}
		
	}

}